<!--
 * @Date: 2024-10-23 10:43:13
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2024-10-23 11:33:57
 * @Description: 项目选择表格
-->
<template>
  <rk-container :isFooter="false" :isAside="false" :isHeader="false">
    <template #main>
      <rk-center>
        <template #condition>
          <rk-center-bar>
            <template #content>
              <el-form :inline="true" label-width="80px">
                <el-form-item label="项目类型">
                  <RkTreeSelect class="mr-15px w-200px"
                    v-model="tableParams.proType"
                    dist='DSY_V_ELE_ZWXMLX'>
                  </RkTreeSelect>
                </el-form-item>
                <el-form-item label="建设状态">
                  <RkTreeSelect class="mr-15px w-200px"
                    v-model="tableParams.proStatusCode"
                    :filterable="false"
                    :showCode="false"
                    :dist="{
                      name: 'DEBT_V_CTS_ELE_XMJSZT',
                      params: { condition: ' AND GUID IN (1,2)' }
                    }">
                  </RkTreeSelect>
                </el-form-item>
                <el-form-item label="模糊查询">
                  <el-input class="mr-15px w-200px"
                    placeholder="请输入项目编码/名称"
                    v-model="tableParams.mhcx"
                    clearable></el-input>
                </el-form-item>
              </el-form>
            </template>
            <template #buttons>
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <el-button @click="handleReset">重置</el-button>
            </template>
          </rk-center-bar>
        </template>
        <template #content>
          <rk-grid
            ref="tableRef"
            :="tableAttrs"
            :params="tableParams"
            :columns="tableColumns"
          >
          </rk-grid>
        </template>
      </rk-center>
    </template>
  </rk-container>
</template>

<script setup>
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';
import { defAxios as request, encryptAxios } from '@/utils/http';

const props = defineProps({
  adCode: { type: String },
  agCode: { type: String },
  processKey: { type: String },
})

const tableRef = ref()
const tableAttrs = reactive({
  dataUrl: '/proResPerfect/proList',
  keyField: 'proId',
  boxConfig: { mode: 'radio' , fixed: true },
  toolbarConfig: false
})
const tableParams = reactive({
  // 模糊查询
  mhcx: undefined,
  // 流程节点
  nodeId: 'node_24',
  // 区划编码
  adCode: props.adCode,
  // 单位编码
  agCode: props.agCode,
  // 项目类型
  proType: undefined,
  // 流程编码
  processKey: props.processKey,
  // 建设状态
  proStatusCode: undefined,
})
const tableColumns = reactive([
  { field: 'agencyName', title: '申报单位', width: 180, align: 'left', },
  { field: 'proName', title: '项目名称', width: 250, align: 'left', cellRender: { name: 'xmYHS' } },
  { field: 'proCode', title: '项目编码', width: 200, align: 'left', },
  { field: 'fiscalYear', title: '年度', width: 120, align: 'center', },
  { field: 'debtDeadlineName', title: '债券期限', width: 130, align: 'center', },
  {
    field: 'applyAmt',
    title: '申请金额（万元）',
    formatter: 'formatThousand',
    width: 150,
    align: 'right'
  },
  {
    field: 'proCapital',
    title: '项目资本金（万元）',
    formatter: 'formatThousand',
    width: 150,
    align: 'right'
  },
  { field: 'bondTypeName', title: '申请类型', width: 150, align: 'left' },
  { field: 'applyDate', title: '申报日期', width: 120, align: 'center' },
  { field: 'setupYear', title: '立项年度', width: 100, align: 'center' },
  { field: 'proNatureName', title: '项目性质', width: 150, align: 'left' },
  { field: 'proTypeName', title: '项目类型', width: 150, align: 'left' },
  { field: 'fundInvestAreaName', title: '资金投向领域', width: 200, align: 'left' },
  { field: 'startDate', title: '开工日期', width: 120, align: 'center' },
  { field: 'endDate', title: '竣工日期', width: 120, align: 'center' },
  { field: 'proStatusName', title: '建设状态', width: 120, align: 'left' },
])
/**
 * 重置
 */
const handleReset = () => {
  tableParams.mhcx = undefined
  tableParams.proType = undefined
  tableParams.proStatusCode = undefined
  handleSearch()
}
/**
 * 查询
 */
const handleSearch = () => {
  tableRef.value.searchEvent()
}
/**
 * 获取选中数据
 */
const getSelectedRow = async () => {
  const selectedRow = tableRef.value.getRadioRecord()
  if (!selectedRow) {
    ElMessage.warning("请选择一条数据进行操作！")
    return
  }
  // 检查是否有变更在途数据
  const hasAlterDoing = await checkHasAlterDoing(selectedRow.proId)
  if (!hasAlterDoing) return
  return selectedRow
}
/**
 * 检查是否有变更在途数据
 * 
 * @param proId 项目ID
 */
const checkHasAlterDoing = async (proId) => {
  try {
    const res = await request({
        url: '/proResPerfect/checkProModifyIsEnd',
        method: 'POST',
        data: { proId },
    })
    if (res.code === 200) {
      return true
    } else {
      console.error(res.msg)
      ElMessage.warning(res.msg);
      return false
    }
  } catch(error) {
    console.error(error)
    return false
  }
}

defineExpose({
  getSelectedRow
})

</script>

<style lang="scss" scoped>

</style>